<template>
  <div class="app-container">
    <basic-query-form ref="queryForm" @doQuery="getList"  @resetQuery="getList" :dept-type="2">
      <template v-slot:queryItem="data">
        <el-form-item label="述职报告" >
          <el-input
            v-model="data.queryParams.reportName"
            placeholder="请输入报告名"
            clearable
            size="small"
          />
        </el-form-item>
      </template>
    </basic-query-form>


    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['kh:report:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['kh:report:remove']"
        >删除</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="reportList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="述职报告id" align="center" prop="id" />-->
      <el-table-column label="述职报告名" align="center" prop="reportName" />
      <el-table-column label="述职报告路径" align="center" prop="reportPath" />
      <el-table-column label="所属部门" align="center" prop="deptName" />
      <el-table-column label="所属考核计划" align="center" prop="assessmentName" />
      <el-table-column label="所属考核方案" align="center" prop="programmeName"  />
      <el-table-column label="所属被考核人" align="center" prop="khUserName"  />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['kh:report:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['kh:report:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getListByQueryForm"
    />

    <!-- 添加或修改述职报告信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="述职报告名" prop="reportName">
          <el-input v-model="form.reportName" placeholder="请输入述职报告名" />
        </el-form-item>
        <el-form-item label="述职报告路径" prop="reportPath">
          <el-input  v-model="form.reportPath" placeholder="请输入述职报告路径"  disabled/>
        </el-form-item>

        <el-form-item label="所属考核计划">
          <template v-if="this.xinzengShow === true">
            <el-select style="width: 100%" v-model="form.assessmentId" @change="getSubProgramm" placeholder="请选择" >
              <el-option
                v-for="item in AssessmentOptions"
                :key="item.value"
                :label="item.name"
                :value="item.value"
              ></el-option>
            </el-select>
          </template>
          <el-input v-model="form.assessmentName"  v-else disabled/>
        </el-form-item>
        <el-form-item label="所属考核方案">
          <template v-if="this.xinzengShow  === true  && (form.assessmentId)">
            <el-select style="width: 100%" v-model="form.programmeId" @change="getPersonList" placeholder="请选择" >
              <el-option
                v-for="item in ProgrammeOptions"
                :key="item.value"
                :label="item.name"
                :value="item.value"
              ></el-option>
            </el-select>
          </template>
          <el-input v-model="form.programmeName"   v-else disabled/>
        </el-form-item>
        <el-form-item label="所属部门">
          <template v-if="this.xinzengShow  === true && (form.assessmentId)">
            <el-select style="width: 100%" v-model="form.deptId" @change="getPersonList" placeholder="请选择" >
              <el-option
                v-for="item in deptOptions"
                :key="item.value"
                :label="item.name"
                :value="item.value"
              ></el-option>
            </el-select>
          </template>
          <el-input v-model="form.deptName"   v-else disabled/>
        </el-form-item>
        <el-form-item label="所属用户">
          <template v-if="this.xinzengShow  === true && (form.assessmentId)">
            <el-select style="width: 100%" v-model="form.khPersonId" @change="changePerson" placeholder="请选择" >
              <el-option
                v-for="item in personOptions"
                :key="item.id"
                :label="item.userName"
                :value="item.id"
              ></el-option>
            </el-select>
          </template>
          <el-input v-model="form.khUserName"   v-else disabled/>
        </el-form-item>
        <el-form-item label="上传文件">
          <el-upload
            :show-file-list="false"
            :action=uploadImgUrl
            :headers="headers"
             accept='.doc'
            :on-success="fileSuccess">
            <el-button size="mini" type="warning" icon="el-icon-folder-add">上传</el-button>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import BasicQueryForm from '../../components/BasicQueryForm'
  import { getToken } from '@/utils/auth'
  import { listReport, getReport, delReport, addReport, updateReport, exportReport } from "@/api/kh/report";
  import { listAllPerson } from "@/api/kh/person";

export default {
  name: "Report",
  data() {
    return {
      xinzengShow:false,
      headers: {
        Authorization: 'Bearer ' + getToken()
      },
      uploadImgUrl: process.env.VUE_APP_BASE_API + "/common/upload", // 上传的图片服务器地址
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 述职报告信息表格数据
      reportList: [],
      //考核列表
      AssessmentOptions:[],
      //考核列表
      ProgrammeOptions:[],
      //被考核部门
      deptOptions:[],
      personOptions:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form:{
        id: null,
        reportName: '',
        reportPath: '',
        deptId: '',
        assessmentId: '',
        programmeId: '',
        khPersonId: '',
        createBy: '',
        createTime: '',
        updateBy: '',
        updateTime: ''
      },
      // 表单校验
      rules: {
      }
    };
  },
  components: {
    'BasicQueryForm' : BasicQueryForm
  },
  created() {

  },
  methods: {
    getListByQueryForm(){
      this.$refs.queryForm.pageQuery(this.queryParams)
    },
    fileSuccess(res) {
      //文件上传成功
      if(res.code=='200'){
        this.msgSuccess("上传成功");
        debugger
        this.form.reportPath = res.fileName
      }else {
        this.msgError(res.msg);
        return
      }
      // this.addForm.deviceScrapEquipmentDocumentList.push({ fileName: res.fileName, filePath: res.filePath})
    },
    getSubProgramm(){
        let curAssess = this.AssessmentOptions.find(item=> item.value === this.form.assessmentId);
        if(!curAssess) return;
        this.ProgrammeOptions = curAssess.programmes;
        this.deptOptions = curAssess.depts;
        this.form.programmeId = undefined;
        this.form.deptId = undefined;
        this.form.khPersonId = undefined;
        this.getPersonList();
    },
    getPersonList() {
      this.loading = true;
      listAllPerson({
        "deptId": this.form.deptId,
        "assessmentId": this.form.assessmentId,
        "programmeId": this.form.programmeId
      }).then(response => {
        this.personOptions = response.data;
        if (this.personOptions && this.personOptions.length > 0){
          let person = this.personOptions[0];
          this.setFormByPerson(person);
        } else {
          this.form.khPersonId = undefined;
        }
        this.loading = false;
      });
    },
    changePerson(){
      let curPerson = this.personOptions.find(item=> item.value === this.form.khPersonId);
      if(!curPerson) return;
      this.setFormByPerson(curPerson);

    },
    setFormByPerson(person){
      this.form.deptId = person.deptId;
      this.form.assessmentId = person.assessmentId;
      this.form.programmeId = person.programmeId;
      this.form.khPersonId = person.khPersonId;
    },
    /** 查询述职报告信息列表 */
    getList(queryParams) {
      this.queryParams.pageNum = queryParams.pageNum;
      this.queryParams.pageSize = queryParams.pageSize;
      this.loading = true;
      listReport(queryParams).then(response => {
        this.reportList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        reportName: '',
        reportPath: '',
        deptId: '',
        assessmentId: '',
        programmeId: '',
        khPersonId: '',
        createBy: '',
        createTime: '',
        updateBy: '',
        updateTime: ''
      };
      this.resetForm("form");
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.xinzengShow = true
      this.open = true;
      this.title = "添加述职报告信息";
      this.AssessmentOptions = this.$refs.queryForm.AssessmentOptions;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.xinzengShow = false
      this.reset();
      const id = row.id || this.ids
      getReport(id).then(response => {
        this.form = response.data;
        this.getPersonList();
        this.open = true;
        this.title = "修改述职报告信息";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateReport(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getListByQueryForm();
              } else {
                this.msgError(response.msg);
              }
            });
          } else {
            addReport(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getListByQueryForm();
              } else {
                this.msgError(response.msg);
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      let rowsDeleted = this.$CrudUtils.rowsDeleted(
        row, this.ids, this.reportList, o=>o.id, o=>o.reportName
      )
      this.$confirm('是否确认删除述职报告['+ rowsDeleted.msgs +']?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delReport(rowsDeleted.ids);
        }).then(() => {
          this.getListByQueryForm();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有述职报告信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportReport(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>
